#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @Project : chat_model 
# @File    : file_extract_logic.py
# @IDE     : PyCharm 
# @Author  :ZH
# @Time    : 2025/1/14 10:55
__all__ = ['file_extract_logic']

from tortoise.expressions import Q

from apps import cruds
from apps.api.v1.file_extract_api.schema import FileExtractListRequests
from apps.models import User


class FileExtractLogic:
    def __init__(self):
        self.crud = cruds.file_extract_crud

    async def get_default_file_extract(self):
        return await self.crud.model.filter(is_delete=False, is_default=True).order_by('-id').first()

    async def get_extract_object(self, extract_id: int):
        return await self.crud.get_one(is_delete=False, id=extract_id)

    async def file_extract_list(self, query: FileExtractListRequests):
        skip = (query.page - 1) * query.limit
        query_sql = self.crud.model.filter(is_delete=False)
        if query.name:
            query_sql = query_sql.filter(model_name_zh__icontains=query.name)
        count_ = await query_sql.count()
        if count_:
            data = await query_sql.order_by('-id').offset(skip).limit(query.limit).all()
        else:
            data = []
        return count_, data


file_extract_logic = FileExtractLogic()
